import requests
import time
import sys
sys.path.append("../")
from model.StockBaseInfo import *
from frameworks.utils.PrettyTableUtil import *
from frameworks.cores.LoadClass import *
from controller.ApiController import *
import json
import time

loader = LoadClass()

"""
#f2:最新价 f3:涨幅 f4:涨跌额 f5:成交量 f6成交额 f7:振幅
#f8:换手率 f9:市盈率 f10:量比 f11: f12:代码 f14:股票名称 f15:最高
#f16:最低 f17:今开 f18:昨收 f20:总市值 f21:流通市值
#f22:
"""

base = StockBaseInfo()
def getMiniteData(code):
    time.sleep(0.1)
    klineday = base.getDfcfLineDay(code,10)
    if "klines" in klineday:
        i = 1
        score = 0
        startClose = 0
        endClose = 0
        zf = 0
        for row in klineday["klines"]:
            daydata = row.split(",")
            zf = round(float(daydata[8]),2)
            if i == 1:
                startClose = float(daydata[2])
                endClose = float(daydata[2])
            else:
                endClose = float(daydata[2])
            i += 1
        score = round((endClose - startClose) * 100 / startClose, 2)
        return [score, round(zf, 2)]
    return 0

codes = []
apiController = ApiController()
numlist = [1,2,3,9]
for num in numlist:
    prettyTable = PrettyTableUtil(["证券代码", "证券名称", "score", "涨幅"])
    newParam = {}
    newParam["num"] = num
    obj = apiController.getZhangTingPage(newParam)
    for line in obj:
        score,zf = getMiniteData(line["code"][2:])
        prettyTable.addRow([line["code"],line["codename"],score,zf])
    print(prettyTable.renderTable("score"))
    break

